#include "stdafx.h"
#include "game_camera.h"

namespace rpg {

	GameCamera::GameCamera(const GameCameraDesc &gameCameraDesc) : LevelObject(gameCameraDesc.levelObjectDesc) {
		mCamera = gameCameraDesc.ogreCamera;
		mTargetObject = gameCameraDesc.targetObject;
		mDistanceToTarget = gameCameraDesc.distanceToTarget;

		if(mTargetObject == NULL) {
			mCameraTarget = gameCameraDesc.targetPoint;
		}

		setCameraView(gameCameraDesc.cameraView);

		mCamera->setPosition(mCameraEye * mDistanceToTarget);
		mCamera->lookAt(mCameraTarget);
	}

	CameraView GameCamera::getCameraView(void) const {
		return mCameraView;
	}

	void GameCamera::setCameraView(CameraView cameraView) {
		switch(cameraView) {
			case CAMERA_VIEW_FROM_SOUTH_TO_NORTH:
				mCameraEye = Ogre::Vector3(10.0f, 30.0f, 20.0f).normalisedCopy();
				break;
			case CAMERA_VIEW_FROM_NORTH_TO_SOUTH:
				mCameraEye = Ogre::Vector3(-10.0f, 30.0f, -20.0f).normalisedCopy();
				break;
			case CAMERA_VIEW_FROM_WEST_TO_EAST:
				mCameraEye = Ogre::Vector3(-20.0f, 30.0f, 10.0f).normalisedCopy();
				break;
			case CAMERA_VIEW_FROM_EAST_TO_WEST:
				mCameraEye = Ogre::Vector3(20.0f, 30.0f, -10.0f).normalisedCopy();
				break;
		}

		mCameraView = cameraView;
	}

	LevelObject * GameCamera::getTargetObject(void) {
		return mTargetObject;
	}

	void GameCamera::setTargetObject(LevelObject *targetObject) {
		mTargetObject = targetObject;
	}

	Ogre::Camera * GameCamera::getOgreCamera(void) {
		return mCamera;
	}

	void GameCamera::setOgreCamera(Ogre::Camera *ogreCamera) {
		mCamera = ogreCamera;
	}

	Ogre::Real GameCamera::getDistanceToTarget(void) const {
		return mDistanceToTarget;
	}

	void GameCamera::setDistanceToTarget(Ogre::Real distanceToTarget) {
		mDistanceToTarget = distanceToTarget;
	}

	void GameCamera::update(float deltaTime) {
		Ogre::Vector3 camPos = mCamera->getPosition();
		Ogre::Vector3 newPos = (mCameraEye * mDistanceToTarget) + mCameraTarget;
		Ogre::Vector3 moveVec = (newPos - camPos) * 2.5f;

		if(mTargetObject != NULL)
			mCameraTarget = mTargetObject->getPosition();

		if(moveVec.length() > 0.001f) {
			camPos += moveVec * deltaTime;

			mCamera->setPosition(camPos);
			mCamera->lookAt(mCameraTarget);
		} else {
			camPos = newPos;

			mCamera->setPosition(camPos);
			mCamera->lookAt(mCameraTarget);
		}
	}

}